package com.ooho.messageboard.dao;

import com.ooho.messageboard.model.MessageBean;

import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by hugohoo on 10/15/16.
 */
public class MessageDAO implements GenericDAO<MessageBean, Integer> {
    private Connection conn;

    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/message_board";

            this.conn = DriverManager.getConnection(url, "root", "root");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<MessageBean> find() {
        List<MessageBean> list = new ArrayList<>();

        try(Statement stmt = conn.createStatement()){
            String sql = "select * from message";

            try(ResultSet rs = stmt.executeQuery(sql)){
                while(rs.next()){
                    MessageBean mb = new MessageBean();
                    mb.setId(rs.getInt("id"));
                    mb.setMessage(rs.getString("message"));
                    mb.setAuthor(rs.getString("author"));
                    mb.setTime(rs.getString("time"));

                    list.add(mb);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public MessageBean find(Integer id) {
        MessageBean mb = new MessageBean();

        try(PreparedStatement pstmt = conn.prepareStatement("select * from message where id = ?")){
            pstmt.setInt(1, id);

            try(ResultSet rs = pstmt.executeQuery()){
                if(rs.next()){
                    mb.setId(rs.getInt("id"));
                    mb.setMessage(rs.getString("message"));
                    mb.setAuthor(rs.getString("author"));
                    mb.setTime(rs.getString("time"));
                }else{
                    throw new SQLException("id not found.");
                }
            }
        } catch (SQLException e){
            e.printStackTrace();
        }

        return mb;
    }

    @Override
    public MessageBean insert(MessageBean value) {
        MessageBean mb = new MessageBean();

        try(PreparedStatement pstmt = conn.prepareStatement("insert into message (message, author, time) values (?, ?, ?)", Statement.RETURN_GENERATED_KEYS)){

            Calendar calendar  = Calendar.getInstance();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String newTime = dateFormat.format(calendar.getTime());

            pstmt.setString(1, value.getMessage());
            pstmt.setString(2, value.getAuthor());
            pstmt.setString(3, newTime);

            if(pstmt.executeUpdate() == 1){
                try(ResultSet rs = pstmt.getGeneratedKeys()){
                    rs.next();
                    int newId =  (int)rs.getLong(1);
                    mb.setMessage(value.getMessage());
                    mb.setAuthor(value.getAuthor());
                    mb.setId(newId);
                    mb.setTime(newTime);
                }
            }
        }catch(SQLException e){
            e.printStackTrace();
        }

        return mb;
    }

    @Override
    public int update(MessageBean value) {
        int rslt = -1;

        try(PreparedStatement pstmt = conn.prepareStatement("update message set message = ?, author = ? where id = ?")){
            pstmt.setString(1, value.getMessage());
            pstmt.setString(2, value.getAuthor());
            pstmt.setInt(3, value.getId());

            rslt = pstmt.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }

        return rslt;
    }

    @Override
    public int delete(MessageBean value) {
        int rslt = -1;

        try(PreparedStatement pstmt = conn.prepareStatement("delete from message where id = ?")){
            pstmt.setInt(1, value.getId());

            rslt = pstmt.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }

        return rslt;
    }

    public void close(){
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
